package com.itsu.app.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.impl.DateConverter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.NumberUtil;
import com.itsu.app.interceptor.UserThreadLocal;
import com.itsu.interfaces.entity.Comment;
import com.itsu.interfaces.entity.Publish;
import com.itsu.interfaces.entity.UserInfoEntity;
import com.itsu.interfaces.service.CommentService;
import com.itsu.pojo.enums.CommentType;
import com.itsu.pojo.vo.CommentVo;
import com.itsu.pojo.vo.MessageCommentVo;
import com.itsu.pojo.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AppCommentService {

    @Reference
    CommentService commentService;

    @Autowired
    AppPublishService appPublishService;

    @Autowired
    AppUserInfoService appUserInfoService;

    Long queryLikedCount(Object publishId) {
        return commentService.queryLikedCount(publishId);
    }

    Long queryCommentCount(Object publishId) {
        return commentService.queryCommentCount(publishId);
    }

    Long queryLovedCount(Object publishId) {
        return commentService.queryLovedCount(publishId);
    }

    Long queryCommentStatus(ObjectId publishId, Long userId, Integer commentType){
        return commentService.queryCommentStatus(publishId, userId, commentType);
    }

    Long loveComment(Comment comment) {
        return commentService.saveLoveComment(comment);
    }

    public Long unLoveComment(Comment comment) {
        return commentService.deleteLoveComment(comment);
    }

    public Long likeComment(Comment comment) {
        return commentService.saveLikeComment(comment);
    }

    public Long dislikeComment(Comment comment) {
        return commentService.deleteLikeComment(comment);
    }

    public void addComments(String publishId, String text) {
        Publish publisher = appPublishService.findOnePublish(publishId);
        Comment comment = Comment.builder()
                .publishId(new ObjectId(publishId))
                .userId(UserThreadLocal.getUid())
                .publishUserId(publisher.getUserId())
                .commentType(CommentType.COMMENT.getValue())
                .content(text)
                .isParent(false)
                .parentId(null)
                .created(System.currentTimeMillis())
                .build();
        commentService.saveComment(comment);
    }

    public void addComments(Comment comment) {
        commentService.saveComment(comment);
    }

    public void deleteComments(Comment comment) {
        commentService.deleteComment(comment);
    }

    public PageResult getComments(String publishId, Integer page, Integer pageSize) {
        //todo page 数值问题
        List<Comment> commentList = commentService.getComments(new ObjectId(publishId));
        Integer endIndex = NumberUtil.min(page * pageSize, commentList.size());
        List<Comment> comments = CollUtil.sub(commentList, (page - 1) * pageSize, endIndex);
        //commentList中可能有用户评论多条，不能是用工具类处理
        List<CommentVo> voList = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfoEntity infoEntity = appUserInfoService.findUserByUid(Convert.toLong(comment.getUserId()));
            Long likedCount = commentService.queryLikedCount(comment.getId());
            Long status = commentService.queryCommentStatus(comment.getId(), UserThreadLocal.getUid(), CommentType.LIKED.getValue());
            CommentVo build = CommentVo.builder()
                    .id(comment.getId().toHexString())
                    .avatar(infoEntity.getLogo())
                    .nickname(infoEntity.getNickName())
                    .content(comment.getContent())
                    .createDate(DateUtil.date().toString("HH:MM"))
                    .likeCount(Convert.toInt(likedCount))
                    .hasLiked(Convert.toInt(status))
                    .build();
            voList.add(build);
        }
        return new PageResult(page, pageSize, Convert.toLong(commentList.size()), voList);
    }

    public PageResult getComments(Comment comment, Integer page, Integer pageSize) {
        //todo page 数值问题
        List<Comment> commentList = commentService.getComments(comment);
        Integer endIndex = NumberUtil.min(page * pageSize, commentList.size());
        List<Comment> comments = CollUtil.sub(commentList, (page - 1) * pageSize, endIndex);
        //commentList中可能有用户评论多条，不能是用工具类处理
        List<MessageCommentVo> voList = new ArrayList<>();
        for (Comment com : comments) {
            UserInfoEntity infoEntity = appUserInfoService.findUserByUid(Convert.toLong(com.getUserId()));
            MessageCommentVo messageCommentVo = new MessageCommentVo();
            messageCommentVo.setId(infoEntity.getUserId().toString());
            messageCommentVo.setAvatar(infoEntity.getLogo());
            messageCommentVo.setNickname(infoEntity.getNickName());
            messageCommentVo.setCreateDate(DateUtil.format(infoEntity.getCreated(), "yyyy-MM-dd HH:mm"));
            voList.add(messageCommentVo);
        }
        return new PageResult(page, pageSize, Convert.toLong(commentList.size()), voList);
    }

    public Long commentsLike(ObjectId commentId) {
        Comment comment = commentService.getComment(commentId);
        Comment build = Comment.builder()
                .publishId(commentId)
                .publishUserId(comment.getUserId())
                .userId(UserThreadLocal.getUid())
                .commentType(CommentType.LIKED.getValue())
                .content(null)
                .parentId(null)
                .isParent(false)
                .created(System.currentTimeMillis())
                .build();
        return commentService.saveLikeComment(build);
    }

    public Long commentsDislike(ObjectId commentId) {
        Comment comment = commentService.getComment(commentId);
        Comment build = Comment.builder()
                .publishId(commentId)
                .publishUserId(comment.getUserId())
                .userId(UserThreadLocal.getUid())
                .commentType(CommentType.LIKED.getValue())
                .content(null)
                .parentId(null)
                .isParent(false)
                .created(System.currentTimeMillis())
                .build();
        return commentService.deleteLikeComment(build);
    }
}
